from cmd import *
import json
import threading

#TODO : redifine capabilities
NETPLUG_CAPABILITY_SERVER = 1
NETPLUG_CAPABILITY_CLIENT = 2
NETPLUG_TYPE_TCP = "tcp"
NETPLUG_SPECIFIC_TCP_PORT = 'listenPort'
NETPLUG_SPECIFIC_TCP_IP = 'listenIP'

class netPlugParamsGroup:
    """class that holds a group of netplugparams"""
    def __init__(self,list=[]):
        self._listNetPlugParams = list
        
    def __iter__(self):
        return self._listNetPlugParams.__iter__()
    
    def append(self,netplugparam):
        self._listNetPlugParams.append(netplugparam)
        return True
    
    def remove(self,netplugparam):
        self._listNetPlugParams.remove(netplugparam)
    
    def dumps(self):
        """dump data to string"""
        listForJson = []
        for netPlugParam in self:
            listForJson.append(netPlugParam.dumps())
        return json.dumps(listForJson)
    
    def loads(self,jsonString):
        """import netplugparam data from JSON string"""
        listJson = json.loads(jsonString)
        self._listNetPlugParams = []
        for s in listJson:
            n = netPlugParam()
            n.loads(s)
            self._listNetPlugParams.append(n)
        return True


class netPlugParam:
    """class that holds netplug param data"""
    def __init__(self,name=None,type=None,capabilities=None,specificParams=None):
        """capabilities is integer and specificParams should be dict"""
        self._name = name
        self._type = type
        self._capabilities = capabilities
        self._specific = specificParams
        
    def getName(self):
        return self._name
    
    def getType(self):
        return self._type
    
    def getSpecific(self,name):
        return self._specific[name]
    
    def dumps(self):
        """dump data to string"""
        dicForJson = {}
        if self._name:
            dicForJson['name'] = self._name
        if self._type:
            dicForJson['type'] = self._type
        if self._capabilities:
            dicForJson['capabilities'] = self._capabilities     
        if self._specific:
            dicForJson['specific'] = self._specific  
        return json.dumps(dicForJson)
    
    def loads(self,jsonString):
        """import netplugparam data from JSON string"""
        dicJson = json.loads(jsonString)
        if dicJson.has_key('name'):
            self._name = dicJson['name']
        if dicJson.has_key('type'):
            self._type = dicJson['type']
        if dicJson.has_key('capabilities'):
            self._capabilities = dicJson['capabilities']
        if dicJson.has_key('specific'):
            self._specific = dicJson['specific']
        return True
    
class netPlug(threading.Thread):
    """class prototype for network cnx plugin"""
    def __init__(self,netPlugParam,netserver=None):
        """init function netserver only needed for server mode"""
        threading.Thread.__init__(self)
        self._netPlugParam = netPlugParam
        self._netServer = netserver
    
    def run(self):
        """activate netPlug : enable listenning/sending capabilities (server mode)"""
        return
            
        
    def stop(self):
        """stop netPlug"""
        return
    
    def sendCmd(self,cmd):
        """send cmd through netplug (client mode)
        return response in cmd format
        return None if error"""
        return
    
    def handleCmd(self,cmd):
        return self._netServer.handleCmd(cmd)
    
#import specific netplugs here
from tcp import *
    
